//
//  JDCategoryLeftTabelView.m
//  分类列表联动
//
//  Created by yifo on 2018/8/3.
//  Copyright © 2018年 yanhaiqiang. All rights reserved.
//

#import "JDCategoryLeftTabelView.h"
#import "JDCategoryLeftTableCell.h"

@interface JDCategoryLeftTabelView ()<UITableViewDelegate, UITableViewDataSource>


@end

static NSString *leftCell = @"leftCell";

@implementation JDCategoryLeftTabelView
- (NSArray *)DataArray{
    if (!_DataArray) {
        _DataArray = [[NSArray alloc]init];
    }
    return _DataArray;
}
- (NSArray *)ListArray{
    if (!_ListArray) {
        _ListArray = [[NSArray alloc]init];
    }
    return _ListArray;
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
//        [self LoadData];
        self.delegate = self;
        self.dataSource = self;
        self.rowHeight = 50;
        self.selectedRow = 0;
        [self registerClass:JDCategoryLeftTableCell.class forCellReuseIdentifier:leftCell];
    }
    return self;
}
- (void)LoadData{
    // 公共组
    NSUserDefaults *User = [NSUserDefaults standardUserDefaults];
    if ([self.String isEqualToString:@"1"]) {
        NSDictionary *dic = @{
                            @"kfUserId":[User objectForKey:@"kfId"],
                            @"groupType":@"1",
                            @"PC":@"PC",
                            @"pubPriType":@"0"
                            };
        [HttpsTool get:@"/web/group/recovery/all" params:dic success:^(id response) {
//            NSLog(@"----%@",response);
//            NSLog(@"%@",response[@"msg"]);
            InfoModel *model = [InfoModel yy_modelWithJSON:response];
            self.DataArray = [model.data.list yy_modelToJSONObject];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (model.data.list.count > 0) {
                    InfoDataList *list = model.data.list[0];
                    //添加 字典，将label的值通过key值设置传递
                    NSDictionary *dic = @{ @"name":list.groupId
                                         };
                    //创建通知
                    NSNotification *notification =[NSNotification notificationWithName:@"Ggtongzhi" object:nil userInfo:dic];
                    //通过通知中心发送通知
                    [[NSNotificationCenter defaultCenter] postNotification:notification];
                    [self reloadData];
                }
                
            });
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }else{
        // 个人组
        NSDictionary *dict = @{
                            @"kfUserId":[User objectForKey:@"kfId"],
                            @"groupType":@"1",
                            @"PC":@"PC",
                            @"pubPriType":@"1"
                            };
        [HttpsTool get:@"/web/group/recovery/all" params:dict success:^(id response) {
//            NSLog(@"++++%@",response);
            NSLog(@"%@",response[@"msg"]);
            InfoModel *model = [InfoModel yy_modelWithJSON:response];
            self.ListArray = [model.data.list yy_modelToJSONObject];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (model.data.list.count > 0) {
                    InfoDataList *list = model.data.list[0];
                    //添加 字典，将label的值通过key值设置传递
                    NSDictionary *dic = @{ @"name":list.groupId
                                         };
                    //创建通知
                    NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dic];
                    //通过通知中心发送通知
                    [[NSNotificationCenter defaultCenter] postNotification:notification];
                    [self reloadData];
                }
            });
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    
}
#pragma mark -------UITabelViewdataSource-------
- (NSInteger)numberOfSections {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.String isEqualToString:@"1"]) {
        return self.DataArray.count;
    }else{
        return self.ListArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.String isEqualToString:@"1"]) {
        JDCategoryLeftTableCell *cell = [JDCategoryLeftTableCell cellWithTableView:tableView];
        NSDictionary *dic = self.DataArray[indexPath.row];
        cell.titleLabel.text = dic[@"groupName"];
        tableView.tableFooterView = [UIView new];
        if (self.selectedRow == indexPath.row) {
            cell.leftLine.hidden = NO;
        }else {
            cell.leftLine.hidden = YES;
        }
        return cell;
    }else{
        JDCategoryLeftTableCell *cell = [JDCategoryLeftTableCell cellWithTableView:tableView];
        NSDictionary *dic = self.ListArray[indexPath.row];
        cell.titleLabel.text = dic[@"groupName"];
        tableView.tableFooterView = [UIView new];
        if (self.selectedRow == indexPath.row) {
            cell.leftLine.hidden = NO;
        }else {
            cell.leftLine.hidden = YES;
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.String isEqualToString:@"1"]) {
        NSDictionary *dic = self.DataArray[indexPath.row];
        self.selectedRow = indexPath.row;
        [tableView reloadData];
        if (self.CellSelectedBlock) {
            self.CellSelectedBlock(indexPath,dic[@"groupId"]);
        }
    }else{
        NSDictionary *dic = self.ListArray[indexPath.row];
        self.selectedRow = indexPath.row;
        [tableView reloadData];
        if (self.CellSelectedBlock) {
            self.CellSelectedBlock(indexPath,dic[@"groupId"]);
        }
    }
}
- (void)setSelectedRow:(NSInteger)selectedRow {
    _selectedRow = selectedRow;
    [self reloadData];
}


@end
